package hidalgo.expenses;

import hidalgo.expenses.activity.BaseActivity;
import hidalgo.expenses.activity.LoginActivity;
import hidalgo.expenses.activity.MenuActivity;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends BaseActivity {

	private Timer timer;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		
		SessionManager.restoreLogin();

		timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {

				if (SessionManager.isLogged()) {
					Intent intent = new Intent(MainActivity.this,
							MenuActivity.class);
					startActivity(intent);
					finish();
				}else{
					Intent intent = new Intent(MainActivity.this,
							LoginActivity.class);
					startActivity(intent);
					finish();
				}
			}
		}, 500);

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		if (timer != null) {
			timer.cancel();
			timer.purge();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_activity, menu);
		return true;
	}
}
